#ifndef HAM_SYSTEM_H
#define HAM_SYSTEM_H

#include <QtWidgets/QApplication>
#include "Module.h"

namespace ham
{
	class Application;

	// singleton
	class System
	{
	public:
		~System(void);

		static System* init(int argc, char *argv[]);

		int run();
		
		template <typename T>
		T* registerModule(QWidget* parent = 0);

		QVector<Module*>& getModules();

	private:
		System(int argc, char *argv[]);

		bool init();

		bool removeModule(Module*);
		bool initModule(Module*);
		bool initModules();
		
		void loadState();
		void saveState();

		static System*		m_instance;
		QApplication		m_application;
		Application*		m_mainWindow;
		QVector<Module*>	m_modules;
		QMap<QString, QPair<QByteArray, QByteArray>> m_stateInfo;
	};
	
	// template implementation
	template <typename T>
	T* System::registerModule(QWidget* parent)
	{
		// create a module instance
		T* newModule = new T(parent);

		// check if there is already a module with the same name
		for (int i = 0; i < m_modules.size(); i++) {
			QString name = m_modules[i]->getModuleName();
			if (name == newModule->getModuleName()) {
				delete newModule;
				return 0;
			}
		}

		// save module pointer
		m_modules.push_back(newModule);
		
		return newModule;
	}
}

#endif // HAM_SYSTEM_H